package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.util.UrlPathHelper;
import org.springframework.util.ResourceUtils;

import java.nio.file.Path;
import java.nio.file.Paths;
@SpringBootApplication
public class DemoApplication extends WebMvcConfigurationSupport {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
	Path path = Paths.get("");
	// private String projectPath = path.toAbsolutePath().toString();
	String staticPath = Paths.get("").toAbsolutePath().toString()+"/static";
	@Override
	protected void addResourceHandlers(ResourceHandlerRegistry registry) {
		//重定向设置
		registry.addResourceHandler("/html/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/html/");
		registry.addResourceHandler("/css/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/css/");
		registry.addResourceHandler("/js/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/js/");
		registry.addResourceHandler("/download/**").addResourceLocations("file:"+staticPath +"/");
		registry.addResourceHandler("/preview/**").addResourceLocations("file:"+staticPath +"/");
		// registry.addResourceHandler("/pdfs/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/../../static/pdfs/");
		super.addResourceHandlers(registry);
	}
	@Bean
    public CharacterEncodingFilter characterEncodingFilter(){
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        filter.setForceEncoding(true);
        return filter;
    }
	@Override
	public void configurePathMatch(PathMatchConfigurer configurer)
	{
		UrlPathHelper urlPathHelper = new UrlPathHelper();
		urlPathHelper.setAlwaysUseFullPath(true);
		configurer.setUrlPathHelper(urlPathHelper);

	}
}